// Package myexcel
// @Author: zhangdi
// @File: write_struct_fields
// @Version: 1.0.0
// @Date: 2023/10/19 19:28
package myexcel

import (
	"github.com/carmel/gooxml/spreadsheet"
	"math/rand"
	"reflect"
	"strconv"
	"strings"
)

func G() {

}

// 示例结构体
type User struct {
	ID       int    `xlsx:"ID"`
	Username string `xlsx:"Username"`
	Age      int    `xlsx:"Age"`
}

// 获取结构体字段tag的值
func getTagValue(fieldTag string) string {
	tagValue := strings.Split(fieldTag, ",")[0]
	return tagValue
}

// 获取示例数据
func getExampleData(fieldType string) string {
	switch fieldType {
	case "int":
		return strconv.Itoa(rand.Intn(100))
	case "string":
		return "Example"
	default:
		return ""
	}
}

var ColumnNames = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}

// WriteStructTagJsonToXlsx 将结构体的tag标签和示例数据写入xlsx文件
func WriteStructTagJsonToXlsx(data interface{}, path string) error {
	// 获取结构体的类型和值
	structType := reflect.TypeOf(data)
	// 遍历结构体字段
	ss := spreadsheet.New()
	sheet := ss.AddSheet()

	for i := 0; i < structType.NumField(); i++ {
		fieldTag := structType.Field(i).Tag.Get("json")
		tagValue := getTagValue(fieldTag)
		println(tagValue)
		sheet.Cell(ColumnNames[i] + "1").SetString(tagValue)
		//typeStr := structType.Field(i).Type.Name()
		//if toTsTypeByString(typeStr) == "string" {
		//	sheet.Cell(ColumnNames[i] + "2").SetString(getRandomString())
		//}
		//if toTsTypeByString(typeStr) == "number" {
		//	sheet.Cell(ColumnNames[i] + "2").SetString("6")
		//}
		//if toTsTypeByString(typeStr) == "bool" {
		//	sheet.Cell(ColumnNames[i] + "2").SetString("1")
		//}

	}

	// 保存xlsx文件
	err := ss.SaveToFile(path)
	if err != nil {
		return err
	}

	return nil
}

func toTsTypeByString(golangType string) string {
	switch golangType {
	case "string":
		return "string"
	case "int", "int8", "int16", "int32", "int64",
		"uint", "uint8", "uint16", "uint32", "uint64",
		"float32", "float64":
		return "number"
	case "bool":
		return "boolean"
	case "[]string":
		return "string[]"
	default:
		return "any"
	}
}

func getRandomString() string {
	var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
	b := make([]rune, 10)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}
	return string(b)
}

func getRandomInt() int {
	return rand.Intn(100)
}
